

<template>
  <div class="login-container">
    <div class="navbar"></div>
    <div class="login-content">
      <div class="top">
        <div class="title">欢迎登录</div>
      </div>
      <div class="form-main">
        <a-form
            ref="formRef"
            :model="formState"
            name="basic"
            :label-col="{ span: 8 }"
            :wrapper-col="{ span: 16 }"
            autocomplete="off"
        >
          <a-form-item
              label="账户"
              name="username"
              :rules="[{ required: true, message: 'Please input your username!' }]"
          >
            <a-input v-model:value="formState.username" />
          </a-form-item>

          <a-form-item
              label="密码"
              name="password"
              :rules="[{ required: true, message: 'Please input your password!' }]"
          >
            <a-input-password v-model:value="formState.password" />
          </a-form-item>

          <a-form-item name="remember" :wrapper-col="{ offset: 8, span: 16 }">
            <a-checkbox v-model:checked="formState.remember">记住我</a-checkbox>
          </a-form-item>

          <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
            <a-button type="primary" html-type="submit" @click="onSubmit">登录</a-button>
          </a-form-item>
        </a-form>
      </div>
    </div>
  </div>
</template>
<script setup>
import {reactive, ref} from 'vue';
import axios from "axios";
import {message} from "ant-design-vue";
import {useRouter} from "vue-router";
const router = useRouter()
const formRef = ref(null)
const formState = reactive({
  username: 'user_001',
  password: '123456',
  remember: true,
});
const onSubmit = (values) => {
  formRef.value
      .validate()
      .then(() => {
        axios.post('/api/member/login', formState)
            .then(res => {
              if (res.data.code !== 200) {
                message.error(res.data.msg);
              } else {
                message.success("登录成功");
                console.log('response', res.data);
                localStorage.setItem('token', res.data.data.token);
                localStorage.setItem('username', res.data.data.username);
                localStorage.setItem('userId', res.data.data.id);
                console.log(router)
                router.push({
                  path: '/home'
                })
              }
            })
            .catch(error => {
              console.log('error', error);
            });
      })
      .catch(error => {
        console.log('error', error);
      });
};

</script>

<style scoped lang="less">
.login-container {
  width: 100%;
  min-height: 100%;
  background: #f0f2f5 url('~@/assets/background.ebcb9160.svg') no-repeat 50%;
  background-size: 100%;
  position: relative;
  .navbar {
    width: 100%;
    height: 40px;
    line-height: 44px;
    text-align: right;
  }
  .login-content {
    padding: 32px 0 24px;
    .top {
      text-align: center;
    }
    .title {
      height: 44px;
      line-height: 44px;
      font-size: 32px;
      font-weight: bold;
      margin-bottom: 40px
    }
    .form-main {
      min-width: 260px;
      width: 368px;
      margin: 0 auto;
    }
  }
}

</style>